using System;
using System.Collections.Generic;
using CMS.Core.DataInterfaces;
using CMS.Core.Domain;
using NHibernate;
using NHibernate.Criterion;

namespace CMS.Data
{
    public class DiaBanDao : AbstractNHibernateDao<DiaBan, System.Int32>, IDiaBanDao
    {
        #region Create Instance
        private static DiaBanDao instance = null;
        public static DiaBanDao Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DiaBanDao();
                }
                return instance;
            }
        }
        #endregion     
    
        #region IDiaBanDao Members

        public byte GetMaxSTT(int maDiaBanTrucThuoc)
        {
            byte result = 1;
            try
            {
                Object obj = null;
                String strSQL = @"Select Max(db.ThuTu)from DiaBan db Where db.MaDiaBanTrucThuocLookup.ID = :DIABANLOOKUP";
                obj = NHibernateSessionManager.Instance.GetSession().CreateQuery(strSQL)
                    .SetParameter("DIABANLOOKUP", maDiaBanTrucThuoc)
                    .UniqueResult();
                if (obj != null)
                {
                    result = Convert.ToByte(obj);
                    result += 1;
                }
                return result;
            }
            catch (Exception ex)
            {
                System.Console.Write(ex);
                return result;
            }
        }

        public List<DiaBan> GetAllDiaBanCha()
        {
            List<DiaBan> results;
            ICriteria criteria = NHibernateSessionManager.Instance.GetSession().CreateCriteria(typeof(DiaBan));
            
            criteria.Add(Restrictions.Eq("TrangThai", true));
            criteria.Add(Restrictions.Eq("MaDiaBanTrucThuoc", 0));
            criteria.AddOrder(NHibernate.Criterion.Order.Asc("ThuTu"));

            results = criteria.List<DiaBan>() as List<DiaBan>;

            return results;
        }

        #endregion
    }
}
